class PlayersController < ActionController::Base
	def new
		@game_id = params[:id]
		@position = params[:position]
		@player_id = params[:player_id]
		if @player_id == "0"
			@player = Player.new
		else
			@player = Player.find @player_id
		end
	end
	def create
		if params[:player_id] == "0"
			@player = Player.create(params[:player])
			position = case @player.position
						when 1
							position = 2
						when 2
							position = 1
						when 3
							position = 4
						when 4
							position = 3
						end
			@partner = Player.find_by_game_id_and_position @player.game_id, position
			if @partner
				@player.partner = @partner
				@partner.partner = @player
				@player.save
				@partner.save
			end
			@game = Game.find @player.game_id
			redirect_to play_path(@game.id, @player.id)
		else
			@player = Player.find params[:player_id]
			if @player.password == params[:new_password]
				redirect_to play_path(@game.id, @player.id)
			else
				redirect_to games_path
			end
		end
	end
	def update
		@player = Player.find params[:player_id]
		if @player.password == params[:new_password]
			redirect_to play_path @player.game.id, @player.id
			session[:player_id] = @player.id
		else
			redirect_to games_path
		end
	end
end
